package com.luo;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ManagementSystem {
    private static ArrayList<Student> list = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("----欢迎使用学生管理系统----");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.println("请输入您的选择：");

            String choice = scanner.nextLine();

            //使用switch语句完成操作的选择
            switch (choice) {
                case "1":
                    System.out.println("执行添加学生功能");
                    addStudent();
                    break;
                case "2":
                    System.out.println("执行删除学生功能");
                    deleteStudent();
                    break;
                case "3":
                    System.out.println("执行修改学生功能");
                    modifyStudent();
                    break;
                case "4":
                    System.out.println("执行查询学生功能");
                    seekStudent();
                    break;
                case "5":
                    System.out.println("感谢您的使用，再见！");
                    System.exit(0); // 退出程序
                    break;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }

    public static void seekStudent() {
        System.out.println("-------------------------------------");
        System.out.println("| 学号\t| 姓名\t| 年龄\t| 居住地\t|");
        System.out.println("-------------------------------------");

        //遍历集合并显示学生信息
        for (Student student : list) {
            System.out.printf("| %s\t| %s\t| %d岁\t| %s\t|\n",
                    student.getId(),
                    student.getName(),
                    student.getAge(),
                    student.getAddress());
        }
        System.out.println("-------------------------------------");
    }


    public static void addStudent() {
        System.out.println("请输入学生id：");
        String id = scanner.next();
        if (getIndex(list, id) != -1) {
            System.out.println("该学号已存在，请重新输入！");
            return;
        }
        System.out.println("请输入学生姓名：");
        String name = scanner.next();
        System.out.println("请输入学生年龄：");
        int age = scanner.nextInt();
        System.out.println("请输入学生地址：");
        String address = scanner.next();
        Student student = new Student(id, name, age, address);
        list.add(student);
        System.out.println("添加成功！");
    }
    public static int getIndex(ArrayList<Student> list, String sid) {
        // 遍历学生集合
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            // 比较学号是否匹配
            if (student.getId().equals(sid)) {
                return i; // 找到则返回当前索引
            }
        }
        return -1; // 未找到返回-1
    }
    // 删除学生
    public static void deleteStudent() {
        System.out.print("请输入要删除的学生学号：");
        String sid = scanner.nextLine();

        int index = getIndex(list, sid);
        if (index == -1) {
            System.out.println("该学号不存在，请重新输入！");
        } else {
            list.remove(index);
            System.out.println("删除学生成功！");
        }
    }

    // 修改学生
    public static void modifyStudent() {
        System.out.print("请输入要修改的学生学号：");
        String sid = scanner.nextLine();

        int index = getIndex(list, sid);
        if (index == -1) {
            System.out.println("该学号不存在，修改失败！");
        } else {
            // 获取要修改的学生信息
            Student student = list.get(index);
            // 键盘录入新的学生信息
            System.out.print("请输入学生新姓名（留空不修改）：");
            String newName = scanner.nextLine();
            if (!newName.isEmpty()) {
                student.setName(newName);
            }

            System.out.print("请输入学生新年龄（留空不修改）：");
            String ageInput = scanner.nextLine();
            if (!ageInput.isEmpty()) {
                try {
                    student.setAge(Integer.parseInt(ageInput));
                } catch (NumberFormatException e) {
                    System.out.println("年龄格式不正确，保持原值");
                }
            }

            System.out.print("请输入学生新居住地（留空不修改）：");
            String newAddress = scanner.nextLine();
            if (!newAddress.isEmpty()) {
                student.setAddress(newAddress);
            }
            // ... 获取新信息并修改
            System.out.println("修改学生成功！");
        }
    }
}
